<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>滴答办公系统-班级修改</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport"
	content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="stylesheet" href="media/layui/css/layui.css" media="all">
<script type="text/javascript" src="media/js/jquery.min.js"></script>
<script src="media/layui/layui.js"></script>
</head>
<body>

	<div class="layui-container" style="margin-top: 5px">
		<form class="layui-form" action="gradeupdate.do" method="post">
		<div class="layui-form-item">
				<label class="layui-form-label">班级序号</label>
				<div class="layui-input-block">
					<input type="text" name="id" readonly="readonly" autocomplete="off" class="layui-input" id="id">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">班级名称</label>
				<div class="layui-input-block">
					<input type="text" name="name" id="name" lay-verify="name" autocomplete="off"
						placeholder="请输入名称" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">班级学科</label>
				<div class="layui-input-block">
					<select name="cid" id="cds">
        				<option value="-1">--请选择学科--</option>
        				<option value="-1">Python人工智能</option>
        				<option value="-1">JAVAEE</option>
      				</select>
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">开班日期</label>
				<div class="layui-input-block">
				 <input type="text" name="createdate" id="date" autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">班级周期</label>
				<div class="layui-input-block">
					<input type="text" name="week" id="time" id="f5" lay-verify="name" autocomplete="off"
						placeholder="请输入周期" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">班级地址</label>
				<div class="layui-input-block">
					<input type="text" name="location" id="address" lay-verify="name" autocomplete="off"
						placeholder="请输入地址" class="layui-input">
				</div>
			</div>
			
		
			<div class="layui-form-item">
				<input class="layui-btn"  style="margin-left: 10%" onclick="modClass()" value="确认修改">
			</div>
		</form>
	</div>
	
	<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
	<script>
		layui.use([ 'form', 'laydate' ], function() {
      var form = layui.form, layer = layui.layer, laydate = layui.laydate;
       
       //日期
       laydate.render({
         elem : '#date'
       });
       
       function init() {
         $.ajax({
           url: "/emp/classes/getClasses",
           type: "POST",
           dataType: "json",
           data: {
             id: getQueryVariable("id")
           }, //提交表单的数据
           success: function(res) {
             analyseData(res);
           },
           error: function(XMLHttpRequest, textStatus, errorThrown) {
             alert(XMLHttpRequest.status);
             alert(XMLHttpRequest.readyState);
             alert(textStatus);
           }
         });
       }
       
       init();
       
       function analyseData(data) {
         var id = data.classes.id;
         var major_id = data.classes.major_id;
         var class_name = data.classes.class_name;
         var class_date = data.classes.class_date;
         var class_time = data.classes.class_time;
         var class_address = data.classes.class_address;
         
         $("#id").val(id);
         $("#name").val(class_name);
         $("#date").val(class_date);
         $("#time").val(class_time);
         $("#address").val(class_address);
         
         getMajor(major_id);
       }
       
       function getMajor(major_id) {
         $.ajax({
           url: "/emp/major/getAllMajors",
           type: "POST",
           dataType: "json",
           data: {}, //提交表单的数据
           success: function(res) {
            var options = "<option value=\"-1\">--请选择学科--</option>";
            for(var i = 0; i < res.majorsList.length; i++) {
              if(major_id == res.majorsList[i].id) {
                options += "<option value='" + res.majorsList[i].id + "' selected=\"selected\">" + res.majorsList[i].major_name + "</option>";
              }else{
                options += "<option value='" + res.majorsList[i].id + "'>" + res.majorsList[i].major_name + "</option>";
              }
            }
            // console.log(options);
            $('#cds').empty();
            $("#cds").append(options);
            
            form.render();  
           },
           error: function(XMLHttpRequest, textStatus, errorThrown) {
             alert(XMLHttpRequest.status);
             alert(XMLHttpRequest.readyState);
             alert(textStatus);
           }
         });
       }
		});
    
    function getQueryVariable(variable) {
      var query = window.location.search.substring(1);
      var vars = query.split("&");
      for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) {
          return pair[1];
        }
      }
      return (false);
    }
    
    function modClass() {
      var major_id = $('#cds').val();
      var class_name = $('#name').val();
      var class_date = $('#date').val();
      var class_time = $('#time').val();
      var class_address = $('#address').val();
      
      if(major_id != -1 && class_name != "" && class_date != "" && class_time != "" && class_address != "") {
        $.ajax({
          url: "/emp/classes/modClasses",
          type: "POST",
          dataType: "json",
          data: {
            id: getQueryVariable("id"),
            major_id: major_id,
            class_name: class_name,
            class_date: class_date,
            class_time: class_time,
            class_address: class_address
          }, //提交表单的数据
          success: function(res) {
            window.location.reload();
          },
          error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest.status);
            alert(XMLHttpRequest.readyState);
            alert(textStatus);
          }
        });
      }else{
        alert("请完善班级信息！");
      }
    } 
	</script>
</body>
</html>